library(ggplot2)
library(patchwork)
library(MetBrewer)

# set to desired working directory
setwd("4_IAT")

source("26_PI_favorability_fullsample.R")
source("26_PI_favorability_blackwhite.R")
source("26_PI_favorability_ideowhite.R")

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA27_28.txt")

# Trends Plot (A27) -------------------------------------------------------------------
colors <- met.brewer("Kandinsky", 2)
colors <- rev(colors)

p_full <- ggplot(p_dat_Fsamp_therm, aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year, linetype = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_Fsamp_therm, week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_Fsamp_therm, week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Full Sample") +
  scale_x_date(breaks = unique(p_dat_Fsamp_therm$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.05, .09)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_wht <- ggplot(subset(p_dat_bw_therm, race == "White"), aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_bw_therm, race == "White" & week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_bw_therm, race == "White" & week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Among Whites") +
  scale_x_date(breaks = unique(subset(p_dat_bw_therm, race == "White")$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.05, .09)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())


p_blk <- ggplot(subset(p_dat_bw_therm, race == "Black"), aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_bw_therm, race == "Black" & week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_bw_therm, race == "Black" & week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Among Blacks") +
  scale_x_date(breaks = unique(subset(p_dat_bw_therm, race == "Black")$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(NA, NA)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())

p_con <- ggplot(subset(p_dat_wIdeo_therm, ideo == "Conservative"), aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Conservative" & week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Conservative" & week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Among White Conservatives") +
  scale_x_date(breaks = unique(subset(p_dat_wIdeo_therm, ideo == "Conservative")$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.05, .09)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())

p_mod <- ggplot(subset(p_dat_wIdeo_therm, ideo == "Moderate"), aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Moderate" & week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Moderate" & week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Among White Neutrals\n(Moderates)") +
  scale_x_date(breaks = unique(subset(p_dat_wIdeo_therm, ideo == "Moderate")$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.05, .09)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())

p_lib <- ggplot(subset(p_dat_wIdeo_therm, ideo == "Liberal"), aes(x = week, y = pred, group = year)) +
  geom_vline(color = "red", aes(xintercept = as.Date("2020-5-22"))) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .5) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Liberal" & week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  geom_smooth(data = subset(p_dat_wIdeo_therm, ideo == "Liberal" & week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year, linetype = year), se = FALSE) +
  theme_bw() +
  scale_linetype_manual(values = c("dotdash", "solid"), guide = "none") +
  scale_color_manual(values = colors, name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "", title = "Among White Liberals") +
  scale_x_date(breaks = unique(subset(p_dat_wIdeo_therm, ideo == "Liberal")$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.05, .09)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())



p_full + p_wht + p_blk + p_con + p_mod + p_lib +
  plot_layout(guides = 'collect', nrow = 2) + 
  plot_annotation(caption = "Note: Y-axis range for Black respondents is different due to different attitude levels. Predicted weekly means with 84% confidence intervals.") &  
  theme(plot.tag = element_text(size = 18),
        plot.caption = element_text(size = 14, hjust = 0),
        strip.text = element_text(size = 16),
        axis.title = element_text(size = 16),
        axis.text.x = element_text(size = 14, angle = 45, vjust = .8),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 16),
        legend.position = "bottom")
ggsave("./figures/FigureA27.pdf", width = 14, height = 10)


# Pre-Post Plots (A28) --------------------------------------------------------
p_full <- ggplot(coef_out_Fsamp, aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "Full Sample") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_wht <- ggplot(subset(coef_out_bw, race == "Whites"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "",  title = "Whites") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_blk <- ggplot(subset(coef_out_bw, race == "Blacks"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "",  title = "Blacks") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_con <- ggplot(subset(coef_out_ideo, ideo == "White Conservatives"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White\nConservatives") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_mod <- ggplot(subset(coef_out_ideo, ideo == "White Neutrals\n(Moderates)"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White Neutrals\n(Moderates)") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())

p_lib <- ggplot(subset(coef_out_ideo, ideo == "White Liberals"), aes(x = coef, y = var)) +
  geom_vline(xintercept = 0, linetype = "solid", color = "red") +
  geom_point(aes(color = indx, shape = indx, size = indx)) +
  scale_shape_manual(values = c(16, 18), name = "") +
  geom_linerange(aes(xmin = coef - 1.96*se, 
                     xmax = coef + 1.96*se,
                     color = indx),
                 lwd = .5) +
  scale_color_manual(values = c("black", "grey"), name = "") +
  scale_size_manual(values = c(2, 3), name = "") +
  theme_bw() +
  labs(y = "", x = "", title = "White\nLiberals") +
  scale_x_continuous(labels = numform::ff_num(zero = 0, digits = 2),
                     limits = c(-.13,.13)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 16, hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 16),
        strip.background = element_blank())


# Combine
p_full + p_wht + p_blk + p_con + p_mod + p_lib +
  plot_layout(guides = 'collect', nrow = 2) &
  theme(plot.tag = element_text(size = 18),
        plot.caption = element_text(size = 14, hjust = 0),
        strip.text = element_text(size = 16),
        axis.title = element_text(size = 16),
        legend.text = element_text(size = 16),
        legend.position = "bottom")
ggsave("./figures/FigureA28.pdf", width = 14, height = 10)


# End log file for script -------------------------------------------------
TeachingDemos::txtStop()